package threadDemo09;

public class MyThread extends Thread{
    //表示这个类的所有对象都共享ticket属性
    static int ticket=0; //0-99
    //锁对象一定要是唯一的
    static Object object=new Object();

    @Override
    public void run() {
        while (true){
            //同步代码块
            synchronized (MyThread.class){
                if (ticket<100){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket++;
                    System.out.println(getName()+"卖的第"+ticket+"张票");
                }else {
                    break;
                }
            }
        }
    }
}
